package com.example.smartmirror2;

import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.data.distributed.common.*;
import ohos.data.distributed.device.DeviceFilterStrategy;
import ohos.data.distributed.device.DeviceInfo;
import ohos.data.distributed.user.SingleKvStore;
import ohos.hiviewdfx.HiLog;
import ohos.rpc.*;
import ohos.utils.zson.ZSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.alibaba.fastjson.JSON;

public class SyncServiceAbility extends Ability {
    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        initDB();
    }

    //    分布式数据库
        private SingleKvStore kvStore;

        private KvManager kvManager;

        private void initDB() {
        //创建分布式数据库管理实例
        KvManagerConfig config=new KvManagerConfig(this);
        kvManager = KvManagerFactory.getInstance().createKvManager(config);

        //创建分布式数据库
        Options options=new Options();
        options.setCreateIfMissing(true).setEncrypt(false).setKvStoreType(KvStoreType.SINGLE_VERSION);
        //指定数据库名称
        String dbName="testDb";
        kvStore=kvManager.getKvStore(options,dbName);
    }
    private void saveData(String key, String value){
        kvStore.putString(key,value);
    }
    private String getData(String key){
        return kvStore.getString(key);
    }
    private void syncContact() {
        List<DeviceInfo> deviceInfoList = kvManager.getConnectedDevicesInfo(DeviceFilterStrategy.NO_FILTER);
        List<String> deviceIdList = new ArrayList<>();
        for (DeviceInfo deviceInfo : deviceInfoList) {
            deviceIdList.add(deviceInfo.getId());
        }
        kvStore.sync(deviceIdList, SyncMode.PUSH_PULL);
    }


    //    js FA调用java PA
    MyRemote myRemote = new MyRemote();

    @Override
    protected IRemoteObject onConnect(Intent intent) {

        //返回RemoteObject的对象
        return myRemote.asObject();
    }

    class MyRemote extends RemoteObject implements IRemoteBroker {

        public MyRemote() {
            super("descriptor");
        }

        @Override
        public boolean onRemoteRequest(int code, MessageParcel data, MessageParcel reply, MessageOption option) throws RemoteException {
            switch (code) {
                case 110:
                    String str = data.readString();
                    HashMap map = JSON.parseObject(str, HashMap.class);
/*                    System.out.println(str);
                    System.out.println(map.get("lidu"));
                    System.out.println(map.get("min"));
                    System.out.println(map.get("score"));*/

                    saveData("lidu",map.get("lidu")+"");
                    saveData("min",map.get("min")+"");
                    saveData("score",map.get("score")+"");
                    saveData("information",map.get("information")+"");
                    syncContact();


                    //添加返回数据
                    Map<String, Object> result = new HashMap<String, Object>();
/*                    result.put("lidu", getData("lidu"));
                    result.put("min", getData("min"));
                    result.put("score", getData("score"));
                    result.put("information", getData("information"));*/
                    reply.writeString(ZSONObject.toZSONString(result));
                    break;
                case 111:
//                    System.out.println(getData("score"));
                    //添加返回数据
                    Map<String, Object> result2 = new HashMap<String, Object>();
                    result2.put("lidu", getData("lidu"));
                    result2.put("min", getData("min"));
                    result2.put("score", getData("score"));
                    result2.put("information", getData("information"));
                    reply.writeString(ZSONObject.toZSONString(result2));
                    break;
                default:
                    return false;
            }
            return true;
        }

        @Override
        public IRemoteObject asObject() {
            return this;
        }
    }


}
